// 数组与16进制字符串互转（对负值填坑）
// https://blog.csdn.net/qi15211/article/details/82886277
// 字节数组转换为16进制字符串
export function bytes2HexString(bytes, n) {
  let str = ''
  for (let i = 0; i < n; i++) {
    let tmp = bytes[i].toString(16)
    if (bytes[i] < 0) {
      // 此处填坑，当byte因为符合位导致数值为负时候，需要对数据进行处理
      tmp = (255 + bytes[i] + 1).toString(16)
    }
    if (tmp.length === 1) {
      tmp = '0' + tmp
    }
    str += tmp
  }
  return str
}

// 16进制字符串转换为字节数组
export function hexString2Bytes(str, n) {
  let pos = 0
  let len = n
  if (len % 2 !== 0) {
    return null
  }
  len /= 2
  const arrBytes = []
  for (let i = 0; i < len; i++) {
    const s = str.substr(pos, 2)
    const v = parseInt(s, 16)
    arrBytes.push(v)
    pos += 2
  }
  return arrBytes
}
